from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from dawn.friends.models import *

@login_required
def friends(request):
    groups = FriendGroup.objects.filter(user=request.user)
    friendships = Friendship.objects.filter(user=request.user, is_confirmed=True)
    return render_to_response('friends/friends.html', 
                              {'groups': groups, 'friendships': friendships},
                              context_instance=RequestContext(request))
    
@login_required()
def add_friend_group(request):
    if request.method == 'POST':
        group_name = request.POST['group_name']
        try:
            FriendGroup.objects.get(name=group_name)
        except ObjectDoesNotExist:
            friend_group = FriendGroup(user=request.user, name=group_name)
            friend_group.save()
    return HttpResponseRedirect(request.META['HTTP_REFERER'])